#!/usr/bin/env python
# -*- coding: utf-8 -*-

#===============================================================================
# This script can be run periodically (e.g. as a nightly cronjob) 
# to keep a SQLite database with high churn from growing unnecessarily large. 
#===============================================================================


from os import environ
environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from settings import *

#from django.conf import settings
import os

def vacuum_db():
    from django.db import connection
    cursor = connection.cursor() #@UndefinedVariable
    cursor.execute("VACUUM")
    connection.close()

if __name__ == "__main__":
    print "Vacuuming database..."
    before = os.stat(DATABASE_NAME).st_size
    print "Size before: %s bytes" % before
    vacuum_db()
    after = os.stat(DATABASE_NAME).st_size
    print "Size after: %s bytes" % after
    print "Reclaimed: %s bytes" % (before - after)
